<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>PHP Callbacks</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="ffi.examples-basic.html">« Basic FFI usage</a></li>
      <li style="float: right;"><a href="ffi.examples-complete.html">A Complete PHP/FFI/preloading Example »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ffi.examples.html">范例</a></li>
    <li>PHP Callbacks</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="ffi.examples-callback" class="section">
  <h2 class="title">PHP Callbacks</h2>
  <p class="para">
   It is possible to assign a PHP closure to a native variable of function pointer type
   or to pass it as a function argument:
   <div class="example" id="example-530">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$zend&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">FFI</span><span style="color: #007700">::</span><span style="color: #0000BB">cdef</span><span style="color: #007700">(</span><span style="color: #DD0000">"<br />&nbsp;&nbsp;&nbsp;&nbsp;typedef&nbsp;int&nbsp;(*zend_write_func_t)(const&nbsp;char&nbsp;*str,&nbsp;size_t&nbsp;str_length);<br />&nbsp;&nbsp;&nbsp;&nbsp;extern&nbsp;zend_write_func_t&nbsp;zend_write;<br />"</span><span style="color: #007700">);<br />&nbsp;<br />echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World&nbsp;1!\n"</span><span style="color: #007700">;<br />&nbsp;<br /></span><span style="color: #0000BB">$orig_zend_write&nbsp;</span><span style="color: #007700">=&nbsp;clone&nbsp;</span><span style="color: #0000BB">$zend</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">zend_write</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$zend</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">zend_write&nbsp;</span><span style="color: #007700">=&nbsp;function(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$len</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;global&nbsp;</span><span style="color: #0000BB">$orig_zend_write</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$orig_zend_write</span><span style="color: #007700">(</span><span style="color: #DD0000">"{\n\t"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$orig_zend_write</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$len</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$orig_zend_write</span><span style="color: #007700">(</span><span style="color: #DD0000">"}\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$ret</span><span style="color: #007700">;<br />};<br />echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World&nbsp;2!\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$zend</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">zend_write&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$orig_zend_write</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Hello&nbsp;World&nbsp;3!\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Hello World 1!
{
        Hello World 2!
}
Hello World 3!
</pre></div>
    </div>
   </div>
   Although this works, this functionality is not supported on all libffi platforms, is not efficient
   and leaks resources by the end of request.
   <div class="tip"><strong class="tip">小技巧</strong>
    <p class="simpara">
     It is therefore recommended to minimize the usage of PHP callbacks.
    </p>
   </div>
  </p>
 </div></div></div></body></html>